package com.tts.superstar;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tts.superstar.Database.AccountDatabase;
import com.tts.superstar.View.BaseActivity;

public class RegActivity extends BaseActivity {
    EditText editText1,editText2;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initView();
    }

    private void initView() {
        editText1 = findViewById(R.id.edit_account_reg);
        editText2 = findViewById(R.id.edit_password_reg);
        button = findViewById(R.id.btn_reg);
        initLisn();
    }

    private void initLisn() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccountDatabase accountDatabase = new AccountDatabase(RegActivity.this);
                SQLiteDatabase db = accountDatabase.getWritableDatabase();
                Cursor result = db.rawQuery("select zhanghao from account", new String[]{});
                if(result.getCount() == 0){
                    ContentValues values = new ContentValues();
                    values.put("zhanghao",editText1.getText().toString());
                    values.put("mima",editText2.getText().toString());
                    db.insert("account",null,values);
                    db.close();
                    accountDatabase.close();
                    finish();
                }else {
                    Toast.makeText(RegActivity.this, "此账号已存在", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}
